package com.example.zbh.dsliao.ui.activty;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.EMConnectionListener;
import com.easemob.EMError;
import com.easemob.EMEventListener;
import com.easemob.EMNotifierEvent;
import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.application.App;
import com.example.zbh.dsliao.presenter.BasePresenter;
import com.example.zbh.dsliao.thread.ThreadPool;
import com.example.zbh.dsliao.ui.fragment.ContactListFragment;
import com.example.zbh.dsliao.ui.fragment.ConversarionListFragment;
import com.example.zbh.dsliao.ui.fragment.MusicListFragment;
import com.example.zbh.dsliao.ui.fragment.SettingFragment;
import com.example.zbh.dsliao.utils.DliaoUtils;
import com.example.zbh.dsliao.utils.PreferenceHelper;
import com.example.zbh.dsliao.view.CustomChangeDialog;
import com.example.zbh.dsliao.view.CustomDialog;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 主界面activity
 */
public class MainActivity extends BaseFragmentActivity implements View.OnClickListener, EMConnectionListener{

    private Button[] mTabs;
    private Fragment[] fragments;
    private int index;//设置button当前的的index
    private ConversarionListFragment conversarionListFragment;//会话界面
    private ContactListFragment contactListFragment;//联系人界面
    private MusicListFragment musicListFragment;//音乐界面
    private SettingFragment settingFragment;//设置界面
    private CustomChangeDialog dialog;
    private TextView newMessageTv;
    private ThreadPool pool;


    @Override
    protected int getLayOut() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mTabs = new Button[4];
        mTabs[0] = findeView(R.id.btn_conversation);
        mTabs[1] = findeView(R.id.btn_address_list);
        mTabs[2] = findeView(R.id.btn_music);
        mTabs[3] = findeView(R.id.btn_setting);
        mTabs[0].setSelected(true);
        newMessageTv = findeView(R.id.newMessageTv);
    }


    @Override
    protected void initData() {

        pool =ThreadPool.getInstance();

        EventBus.getDefault().register(this);

        //当掉线是,sdk会自动重连,无需进行任何操作

        EMChatManager.getInstance().addConnectionListener(this);
        mTabs[0].setOnClickListener(this);
        mTabs[1].setOnClickListener(this);
        mTabs[2].setOnClickListener(this);
        mTabs[3].setOnClickListener(this);
        conversarionListFragment = new ConversarionListFragment();
        contactListFragment = new ContactListFragment();
        musicListFragment = new MusicListFragment();
        settingFragment = new SettingFragment();
        fragments = new Fragment[]{conversarionListFragment, contactListFragment, musicListFragment, settingFragment};
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, conversarionListFragment).commit();

    }


    private int data;
    private int unReadMessageCount = 0;
    /**
     * 会话按钮上的红色提示
     * 消息未读(总数)
     **/
    public void onEvent(Integer data) {
        this.data = data;
        newMessageTv.setVisibility(View.VISIBLE);
        if (data != 0) {
            if (data <= 99) {
                newMessageTv.setText(data + "");
            } else {
                newMessageTv.setText("99+");
            }
        } else if (data == 0) {
            newMessageTv.setVisibility(View.GONE);
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_conversation:
                index = 0;
                break;
            case R.id.btn_address_list:
                index = 1;
                break;
            case R.id.btn_music:
                index = 2;
                break;
            case R.id.btn_setting:
                index = 3;
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragments[index]).commit();
        for (int i = 0; i < 4; i++) {
            if (index == i) {
                mTabs[i].setSelected(true);
            } else {
                mTabs[i].setSelected(false);
            }
        }
    }


    //连接成功
    @Override
    public void onConnected() {
        Toast.makeText(this, "已连接到服务器", Toast.LENGTH_SHORT).show();
    }

    private boolean isSend;

    @Override
    public void onDisconnected(final int i) {
        isSend = true;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (i == EMError.USER_REMOVED) {
                    if (isSend = true) {
                        EventBus.getDefault().post(101);
                        isSend = false;
                    }
                } else {
                    if (i == EMError.CONNECTION_CONFLICT) {
                        if (isSend = true) {
                            EventBus.getDefault().post(102);
                            isSend = false;
                        }
                    } else {
                        if (DliaoUtils.isNetWorkConnected(MainActivity.this)) {
                            //连接不到服务器
                        } else {
                            //当前网络不可用
                        }
                    }
                }
            }
        });
    }






    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        pool.shutDownPool();
    }



}